package com.example.lets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.listener.FindListener;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class UserList extends BaseActivity {

    ListView listView;
    ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_list);

        listView = (ListView) findViewById(R.id.search_user_view);

        Intent in = getIntent();
        String mode = in.getStringExtra("mode");
        String input = in.getStringExtra("input");
        if (mode.equals("follower"))
            searchFollower(input);
        else if (mode.equals("following"))
            searchFollowing(input);
        else if (mode.equals("participant"))
            searchParticipants(input);

        Toolbar toolbar = (Toolbar) this.findViewById(R.id.search_result_toolbar);
        toolbar.setTitle(getIntent().getStringExtra("title"));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    void searchFollower(String id) {
        BmobQuery<FollowRelation> query = new BmobQuery<FollowRelation>();
        query.addWhereEqualTo("userId", id);
        query.findObjects(UserList.this, new FindListener<FollowRelation>() {
            @Override
            public void onSuccess(List<FollowRelation> list) {
                if (list.size() == 0) return;
                FollowRelation r = list.get(0);
                BmobQuery<User> q = new BmobQuery<User>();
                q.addWhereRelatedTo("follower", new BmobPointer(r));
                q.findObjects(UserList.this, new FindListener<User>() {

                    @Override
                    public void onSuccess(List<User> object) {
                        // TODO Auto-generated method stub
                        for (User user : object) {
                            HashMap<String, Object> map = new HashMap<String, Object>();

                            map.put("ItemName", user.getNickname());
                            map.put("ItemPicFileName", user.getPicFilename());
                            map.put("ItemTag1", user.getTag1());
                            map.put("ItemTag2", user.getTag2());
                            map.put("ItemTag3", user.getTag3());
                            map.put("ItemId", user.getObjectId());

                            listItem.add(map);
                        }
                        listView.setAdapter(new UserListAdapter(UserList.this, listItem));
                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent();
                                intent.putExtra("username", listItem.get(i).get("ItemName").toString());
                                intent.putExtra("userid", listItem.get(i).get("ItemId").toString());
                                intent.putExtra("userpicname", listItem.get(i).get("ItemPicFileName").toString());
                                intent.setClass(UserList.this, UserPageActivity.class);
                                startActivity(intent);
                            }
                        });
                    }

                    @Override
                    public void onError(int code, String msg) {
                        // TODO Auto-generated method stub
                        Log.i("life", "查询失败：" + code + "-" + msg);
                    }
                });
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    void searchFollowing(String id) {
        BmobQuery<FollowRelation> query = new BmobQuery<FollowRelation>();
        query.addWhereEqualTo("userId", id);
        query.findObjects(UserList.this, new FindListener<FollowRelation>() {
            @Override
            public void onSuccess(List<FollowRelation> list) {
                if (list.size() == 0) return;
                FollowRelation r = list.get(0);
                BmobQuery<User> q = new BmobQuery<User>();
                q.addWhereRelatedTo("following", new BmobPointer(r));
                q.findObjects(UserList.this, new FindListener<User>() {

                    @Override
                    public void onSuccess(List<User> object) {
                        // TODO Auto-generated method stub
                        for (User user : object) {
                            HashMap<String, Object> map = new HashMap<String, Object>();

                            map.put("ItemName", user.getNickname());
                            map.put("ItemPicFileName", user.getPicFilename());
                            map.put("ItemTag1", user.getTag1());
                            map.put("ItemTag2", user.getTag2());
                            map.put("ItemTag3", user.getTag3());
                            map.put("ItemId", user.getObjectId());

                            listItem.add(map);
                        }
                        listView.setAdapter(new UserListAdapter(UserList.this, listItem));
                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                Intent intent = new Intent();
                                intent.putExtra("username", listItem.get(i).get("ItemName").toString());
                                intent.putExtra("userid", listItem.get(i).get("ItemId").toString());
                                intent.putExtra("userpicname", listItem.get(i).get("ItemPicFileName").toString());
                                intent.setClass(UserList.this, UserPageActivity.class);
                                startActivity(intent);
                            }
                        });
                    }

                    @Override
                    public void onError(int code, String msg) {
                        // TODO Auto-generated method stub
                        Log.i("life", "查询失败：" + code + "-" + msg);
                    }
                });
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    void searchParticipants(String id) {
        BmobQuery<Involvement> bmobQuery = new BmobQuery<Involvement>();
        bmobQuery.addWhereEqualTo("activityId", id);
        bmobQuery.addQueryKeys("user");
        bmobQuery.include("user");
        bmobQuery.order("createdAt");
        bmobQuery.findObjects(this, new FindListener<Involvement>() {
            @Override
            public void onSuccess(List<Involvement> list) {
                for (Involvement inv : list) {
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    User user = inv.getUser();

                    map.put("ItemName", user.getNickname());
                    map.put("ItemPicFileName", user.getPicFilename());
                    map.put("ItemTag1", user.getTag1());
                    map.put("ItemTag2", user.getTag2());
                    map.put("ItemTag3", user.getTag3());
                    map.put("ItemId", user.getObjectId());

                    listItem.add(map);
                }
                listView.setAdapter(new UserListAdapter(UserList.this, listItem));
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent intent = new Intent();
                        intent.putExtra("username", listItem.get(i).get("ItemName").toString());
                        intent.putExtra("userid", listItem.get(i).get("ItemId").toString());
                        intent.putExtra("userpicname", listItem.get(i).get("ItemPicFileName").toString());
                        intent.setClass(UserList.this, UserPageActivity.class);
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search_result, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        finish();
        return;
    }
}